<?php

define('JaxRAW',0);
define('JaxIS',-1);
define('JaxLIKE',-2);
define('JaxIN',-3);
define('JaxNIN',-4);
define('JaxOR',-5);
define('JaxAND',-6);
define('JaxISNULL',-7);
define('JaxISNOTNULL',-8);
define('JaxNone',-9);
define('JaxCondS',-10);
define('JaxCondE',-11);


class Jax_Search
{
	public $tableName;
	private $searchVars = array();
	public $orderBy = false;
	public $database = false;
	public $debug = false;
	public $nodeValue = false;

	function __construct($db,$tableName)
	{
		$this->database = $db;
		$this->tableName = $tableName;
	}

	function searchVar($varName,$varType,$varVal)
	{
		if($varType==JaxIS)
		return "`$varName`='$varVal'";

		else if($varType==JaxRAW)
		return "$varName  $varVal";

		else if($varType==JaxISNULL)
		return "`$varName` IS NULL";

		else if($varType==JaxISNOTNULL)
		return "`$varName` IS NOT NULL";

		else if($varType==JaxLIKE)
		return "`$varName` LIKE '%$varVal%'";

		else if($varType==JaxIN)
		return "`$varName` IN ($varVal)";

		else if($varType==JaxNIN)
		return "`$varName` NOT IN ($varVal)";
		
		else if($varType==JaxCondS)
		return "(";
		
		else if($varType==JaxCondE)
		return ")";
	}

	function addVar($varName,$varType,$varValue=false,$varCond=JaxAND)
	{
		global $Jax,$_Jax;

		$Jax->check($varName);

		$newVar = array();
		$newVar['name'] = $varName;
		$newVar['type'] = $varType;
		$newVar['value'] = $varValue ? $varValue : $_Jax[$varName];
		$newVar['cond'] = $varCond;

		array_push($this->searchVars,$newVar);
	}

	function startCondition($varCond=JaxAND)
	{
		$newVar = array();
		$newVar['name'] = 'false';
		$newVar['type'] = JaxCondS;
		$newVar['cond'] = $varCond;
		$newVar['value'] = true;

		array_push($this->searchVars,$newVar);
	}

	function endCondition()
	{
		$newVar = array();
		$newVar['name'] = 'false';
		$newVar['type'] = JaxCondE;
		$newVar['cond'] = JaxNone;
		$newVar['value'] = true;

		array_push($this->searchVars,$newVar);
	}

	function respond()
	{
		//		header("content-type:text/plain");
		//		print_r($this->searchVars);
		//		exit;

		global $Jax,$_Jax;

		$Jax->check('page',1,JaxI);
		$Jax->check('per_page',25,JaxI);

		$start_at=($_Jax['page']-1)*$_Jax['per_page'];

		$rSQL = "SELECT * \nFROM `{$this->tableName}` ";
		$sSQL = "SELECT COUNT(*) AS 'COUNT', CEIL(COUNT(*)/'{$_Jax['per_page']}') AS 'Pages', '{$_Jax['page']}' AS 'Page' FROM `{$this->tableName}` ";
		$qSQL = '';

		$useWhere = true;

		for($i=0;$i<count($this->searchVars);$i++)
		{
			$var = $this->searchVars[$i];

			if($var['value']!==false)
			{
				$Qvar = self::searchVar($var['name'],$var['type'],$var['value']);

				if($useWhere)
				{
					$qSQL .= "\nWHERE $Qvar ";
					$useWhere=false;
				}
				else if($var['cond']==JaxOR)
				$qSQL .= "\nOR $Qvar";
				else if($var['cond']==JaxAND)
				$qSQL .= "\nAND $Qvar";
				else if($var['cond']==JaxNone)
				$qSQL .= "\n$Qvar";
			}
		}


		$rSQL .= $qSQL;

		if($this->orderBy)
		$rSQL .= "\nORDER BY `{$this->orderBy}`";

		if($_Jax['per_page'])
		$rSQL .= "\nLIMIT $start_at,{$_Jax['per_page']}";


		if($this->debug)
		echo $rSQL;

		$results=DB::query($this->database,$rSQL);
		$resultXML=false;


		while($result=$results->fetch())
		{
			if($this->nodeValue)
			{
				$value = $result[$this->nodeValue];
				unset($result[$this->nodeValue]);
				$resultXML.=$Jax->XVar('result',$value,$result,true);
			}
			else
			$resultXML.=$Jax->XVar('result',false,$result,true);
		}

		$sSQL .= $qSQL;


		$count=DB::query($this->database,$sSQL)->fetch();
		$Jax->respond($Jax->XVar("results",$resultXML,array()).$Jax->XVar("search",false,$count));
	}



}